//
//  MenuViewController.m
//  MemberProject
//
//  Created by MAC OS X on 15/4/2.
//  Copyright (c) 2015年 MAC OS X. All rights reserved.
//

#import "MenuViewController.h"
#import "HomePageViewController.h"
#import "ServiceViewController.h"
#import "DiscoveryViewController.h"
#import "MyViewController.h"
#import "BaseNavigationController.h"
#import "UserInfoManager.h"
#import "CityFileManager.h"
#import "UserInsuranceManager.h"

@interface MenuViewController ()<UITabBarControllerDelegate,UINavigationControllerDelegate>
@property(strong,nonatomic)UIView *myTabbar;
@property(strong,nonatomic)NSArray *titleButtons;
@property(strong,nonatomic)NSArray *titleLabels;
@end

@implementation MenuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0)
    {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.automaticallyAdjustsScrollViewInsets=NO;
    

    NSMutableArray *arrM=[NSMutableArray array];
    HomePageViewController *homeVC=[[HomePageViewController alloc]init];
    BaseNavigationController *nav=[[BaseNavigationController alloc]initWithRootViewController:homeVC];
    [arrM addObject:nav];
//
    ServiceViewController *serviceVC=[[ServiceViewController alloc]init];
    nav=[[BaseNavigationController alloc]initWithRootViewController:serviceVC];
    [arrM addObject:nav];
    
    DiscoveryViewController *disVC=[[DiscoveryViewController alloc]init];
    nav=[[BaseNavigationController alloc]initWithRootViewController:disVC];
    [arrM addObject:nav];
    
    MyViewController *myVC=[[MyViewController alloc]init];
    nav=[[BaseNavigationController alloc]initWithRootViewController:myVC];
    [arrM addObject:nav];
    
    self.viewControllers=arrM;
    
    self.delegate=self;
    
    NSArray *arr=@[@"home",@"service",@"discover",@"me"];
    self.tabBar.hidden=YES;
    self.myTabbar = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-49, SCREEN_WIDTH, 49)];
    self.myTabbar.backgroundColor=[UIColor colorWithRed:88/255.0 green:196/255.0 blue:181/255.0 alpha:1];
    [self.view addSubview:self.myTabbar];
    CGFloat btnWidth = SCREEN_WIDTH / 4;
    NSArray *btnTitles = @[@"首页",@"服务台",@"发现",@"我"];
    NSMutableArray *buttons = [[NSMutableArray alloc] init];
    NSMutableArray *labels = [[NSMutableArray alloc] init];
    for (int i = 0; i < btnTitles.count; i++) {
        CGRect btnFrame = CGRectMake(i*btnWidth, 0, btnWidth, 49);
        CGRect lblFrame = CGRectMake(i*btnWidth, 34, btnWidth, 15);
        //
        UIButton *button = ({
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = btnFrame;
            btn.backgroundColor=[UIColor colorWithRed:88/255.0 green:196/255.0 blue:181/255.0 alpha:1];
            [btn addTarget:self action:@selector(onClickBtn:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag = 100+i;
            btn;});
        
        UILabel *label = ({
            UILabel *lbl = [[UILabel alloc] initWithFrame:lblFrame];
            lbl.text = btnTitles[i];
            lbl.textColor=[UIColor whiteColor];
            lbl.backgroundColor = [UIColor clearColor];
            lbl.textAlignment = NSTextAlignmentCenter;
            lbl.font = [UIFont systemFontOfSize:12];
            lbl;
        });
        UIImageView *imageView =({
            UIImageView *img =[[UIImageView alloc]init];
            img.frame =CGRectMake((btnWidth-32)/2+ btnWidth*i, 2, 32, 31);
            img.image =[UIImage imageNamed:[NSString stringWithFormat:@"%@",arr[i]]];
            img;
        });
        [self.myTabbar addSubview:button];
        [self.myTabbar addSubview:label];
        [self.myTabbar addSubview:imageView];
        
        [buttons addObject:button];
        [labels addObject:label];
        
    }
    self.titleButtons = buttons;
    self.titleLabels = labels;
    [self selectedWithIndex:0];

    
    UIView *tempTabBar=self.myTabbar;
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[tempTabBar(==49)]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(tempTabBar)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[tempTabBar]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(tempTabBar)]];
    [tempTabBar setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view setNeedsLayout];

    
    
    /*
    HomePageViewController *homeVC = [[HomePageViewController alloc]initWithNibName:@"HomePageViewController" bundle:nil];
    BaseNavigationController *homeNC = [[BaseNavigationController alloc]initWithRootViewController:homeVC];
    homeNC.delegate = self;
    
    ServiceViewController *serviceVC = [[ServiceViewController alloc]initWithNibName:@"ServiceViewController" bundle:nil];
    BaseNavigationController *serviceNC = [[BaseNavigationController alloc]initWithRootViewController:serviceVC];
    serviceNC.delegate = self;
    
    DiscoveryViewController *disVC = [[DiscoveryViewController alloc]initWithNibName:@"DiscoveryViewController" bundle:nil];
    BaseNavigationController *disNC = [[BaseNavigationController alloc]initWithRootViewController:disVC];
    disNC.delegate = self;
    
    MyViewController *myVC = [[MyViewController alloc]initWithNibName:@"MyViewController" bundle:nil];
    BaseNavigationController *myNC = [[BaseNavigationController alloc]initWithRootViewController:myVC];
    myNC.delegate = self;
    
    NSArray *vcArr = @[homeNC,serviceNC,disNC,myNC];
//    for (UINavigationController *naVC in vcArr) {
//        naVC.navigationBar.barTintColor = classicalGreen;
//    }
    self.viewControllers = vcArr;
    self.delegate = self;
    [self setSelectedIndex:0];
    
    NSArray *imageArr = @[@"tabbar_0.png",@"tabbar_1.png",@"tabbar_2.png",@"tabbar_3.png"];
    for (int i=0; i<titleArr.count; i++) {
        UIImage *image = [[UIImage imageNamed:imageArr[i]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UITabBarItem *item = [self.tabBar.items objectAtIndex:i];
        UIFont* font = [UIFont systemFontOfSize:11];
        NSDictionary* textAttributes = @{NSFontAttributeName:font,
                                         NSForegroundColorAttributeName:[UIColor colorWithRed:148.0f/255.0f green:149.0f/255.0f blue:148.0f/255.0f alpha:1]};
        NSDictionary* selectedTextAttributes = @{NSFontAttributeName:font,
                                                 NSForegroundColorAttributeName:[UIColor colorWithRed:13.0f/255.0f green:215.0f/255.0f blue:180.0f/255.0f alpha:1]};
        [item setTitleTextAttributes:textAttributes forState:0];
        [item setTitle:titleArr[i]];
        [item setImage:image];
    }
     */
    
    //获取用户信息
    [self getUserInfo];
    
    
    

}



#pragma mark 获取用户信息
-(void)getUserInfo
{
    
    
    NSString *isLogin =[[NSUserDefaults standardUserDefaults]valueForKey:ISAlreadyLogin];
    if (isLogin !=nil&&[isLogin isEqualToString:loginYES] )
    {
       NSDictionary *userDic = [[UserInfoManager sharedManager]getLocalLog];
        [UserInfoManager sharedManager].modelMser = [ModelUser getUserForDic:userDic];
        [[UserInsuranceManager sharedManager] setupUserInsurance:^{
            
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - UITabBarControllerDelegate
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    
//    int selectedIndex=(int)tabBarController.selectedIndex;
//    
//    UIViewController *vc=self.viewControllers[selectedIndex];
//    self.title=vc.title;
//    self.navigationItem.title=vc.navigationItem.title;
//    self.navigationItem.titleView=vc.navigationItem.titleView;
//    self.navigationItem.leftBarButtonItem=vc.navigationItem.leftBarButtonItem;
//    self.navigationItem.rightBarButtonItem=vc.navigationItem.rightBarButtonItem;
}

-(void)onClickBtn:(UIButton *)sender{
//    sender.selected=YES;
//    self.selectedIndex=sender.tag-100;
    int tag =sender.tag-100 ;
    if (tag==3) {
        [[UserInfoManager sharedManager]presentLoginViewController:self andIsLogin:^{
            [self selectedWithIndex:tag];
        }];
    }else
    {
        [self selectedWithIndex:tag];
    }
    
    
}

-(void)selectedWithIndex:(NSInteger)index{
    int oldSelectedIndex=(int)self.selectedIndex;
    self.selectedIndex=index;
    for(int i=0;i<self.titleButtons.count;i++){
        UIButton *button=self.titleButtons[i];
        if(i==index){
            button.selected=YES;
            button.backgroundColor=[UIColor colorWithRed:61/255.0 green:137/255.0 blue:128/255.0 alpha:1];
//            UILabel *label=self.titleLabels[i];
//            label.textColor=[UIColor whiteColor];
            
//            UIViewController *vc=self.viewControllers[i];
//            self.title=vc.title;
//            self.navigationItem.title=vc.navigationItem.title;
//            self.navigationItem.titleView=vc.navigationItem.titleView;
//            self.navigationItem.leftBarButtonItem=vc.navigationItem.leftBarButtonItem;
//            self.navigationItem.rightBarButtonItem=vc.navigationItem.rightBarButtonItem;
            UINavigationController *nav=self.viewControllers[i];
            if(oldSelectedIndex==index){
//                //同一个
                [nav popToRootViewControllerAnimated:YES];
            }else{
                [nav popToRootViewControllerAnimated:NO];
            }
//            [nav popToRootViewControllerAnimated:YES];
        }else{
            button.selected=NO;
            button.backgroundColor=[UIColor colorWithRed:88/255.0 green:196/255.0 blue:181/255.0 alpha:1];
//            UILabel *label=self.titleLabels[i];
//            label.textColor=[UIColor colorWithRed:64/255.0 green:144/255.0 blue:135/255.0 alpha:1];
        }
    }
}
@end
